/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.cardapiodigital.data;

import br.com.cardapiodigital.database.ConnectionManager;
import br.com.cardapiodigital.entity.CategoriadosItens;
import br.com.cardapiodigital.exception.CardapioDigitalException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author egberto
 */
public class CategoriadosItensData extends GenericData<CategoriadosItens> {

    @Override
    public void salvar(CategoriadosItens entity) throws CardapioDigitalException {
        PreparedStatement pstmt = null;
        Connection conn = null;

        String sql = "INSERT INTO categoriadositens (descricao) values (?)";

        try {
            conn = ConnectionManager.getConexao();
            pstmt = conn.prepareStatement(sql);
            // preenche os valores ?,?
            pstmt.setString(1, entity.getDescricao());

            pstmt.execute();

        } catch (SQLException ex) {
            Logger.getLogger(CategoriadosItensData.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro na execucao da query" + sql;
            throw new CardapioDigitalException(errorMsg, ex);
        } finally {
            ConnectionManager.close(conn, pstmt);
        }
    }

    @Override
    public void atualizar(CategoriadosItens entity) throws CardapioDigitalException {
        PreparedStatement pstmt = null;
        Connection conn = null;

        String sql = "UPDATE categoriadositens SET descricao = ? WHERE id = ?";

        try {
            conn = ConnectionManager.getConexao();
            pstmt = conn.prepareStatement(sql);
            // preenche os valores ?,?
            pstmt.setString(1, entity.getDescricao());
            pstmt.setInt(2, entity.getId());

            pstmt.execute();

        } catch (SQLException ex) {
            Logger.getLogger(CategoriadosItensData.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro na execucao da query" + sql;
            throw new CardapioDigitalException(errorMsg, ex);
        } finally {
            ConnectionManager.close(conn, pstmt);
        }
    }

    @Override
    public void excluir(int entityId) throws CardapioDigitalException {
        PreparedStatement pstmt = null;
        Connection conn = null;

        String sql = "DELETE FROM categoriadositens WHERE id=?";

        try {
            conn = ConnectionManager.getConexao();
            pstmt = conn.prepareStatement(sql);
            // preenche os valores ?,?
            pstmt.setInt(1, entityId);

            pstmt.execute();

        } catch (SQLException ex) {
            Logger.getLogger(CategoriadosItensData.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro na execucao da query" + sql;
            throw new CardapioDigitalException(errorMsg, ex);
        } finally {
            ConnectionManager.close(conn, pstmt);
        }
    }

    @Override
    public List<CategoriadosItens> buscarTodos() throws CardapioDigitalException {
        String sql = "SELECT * FROM categoriadositens ORDER BY descricao ASC";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<CategoriadosItens> listaEstados = new ArrayList<CategoriadosItens>();

        try {
            conn = ConnectionManager.getConexao();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                int id = rs.getInt("id");
                String descricao = rs.getString("descricao");
                listaEstados.add(new CategoriadosItens(id, descricao));
            }

            return listaEstados;

        } catch (SQLException ex) {
            Logger.getLogger(CategoriadosItensData.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro na execucao da query" + sql;
            throw new CardapioDigitalException(errorMsg, ex);
        } finally {
            ConnectionManager.close(conn, stmt, rs);
        }
    }

    @Override
    public CategoriadosItens obtem(int entityId) throws CardapioDigitalException {
        String sql = "SELECT * FROM categoriadositens WHERE id='" + entityId + "'";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        CategoriadosItens catItem = null;

        try {
            conn = ConnectionManager.getConexao();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                int id = rs.getInt("id");
                String descricao = rs.getString("descricao");
                catItem = new CategoriadosItens(id, descricao);
            }

            return catItem;

        } catch (SQLException ex) {
            Logger.getLogger(CategoriadosItensData.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro na execucao da query " + sql;
            throw new CardapioDigitalException(errorMsg, ex);
        } finally {
            ConnectionManager.close(conn, stmt, rs);
        }
    }
}
